package com.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chen.entity.Book;
import com.chen.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BookController {

    @Autowired
    IBookService bookService;

    @GetMapping("/getAlls")
    public String getAll(Model model) {
        List<Book> list = bookService.list();
        model.addAttribute("list", list);
        return "books";
    }


    @GetMapping("/getSearch")
    public String getBook(Model model, Book book) {
        List<Book> searchbooks = bookService.searchbooks(book);
        model.addAttribute("book", book);
        model.addAttribute("list", searchbooks);
        return "books";
    }


    @GetMapping("/getPage")
    public String getPage(Model model, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, Book book) {
        IPage page = bookService.getPage(pageNum, book);
        model.addAttribute("page", page);
        model.addAttribute("book", book);
        return "books";
    }
}
